package org.freez4.spring.resource;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.io.Reader;
import java.util.logging.Logger;

/**
 * 带有字符编码的 {@link FileSystemResource} 示例
 *
 * @author 阿坤同学
 * @date 2024/1/17 4:35 PM
 */
public class EncodedFileSystemResourceDemo {

    public static final Logger logger = Logger.getLogger(EncodedFileSystemResourceDemo.class.getName());

    public static void main(String[] args) throws IOException {
        String currentJavaFilePath = System.getProperty("user.dir") + "/springframework/resource/src/main/java/org/freez4/spring/resource/EncodedFileSystemResourceDemo.java";
        // FileSystemResource => WritableResource => Resource
        FileSystemResource fileSystemResource = new FileSystemResource(currentJavaFilePath);
        EncodedResource encodedResource = new EncodedResource(fileSystemResource, "UTF-8");
        // 字符输入流
        try (Reader reader = encodedResource.getReader()) {
            String content = String.format("EncodedFileSystemResourceDemo.java 源码:%s", IOUtils.toString(reader));
            logger.info(content);
        }
    }
}